﻿using System;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Linq;
using System.Windows;
using System.Collections.ObjectModel;

namespace QuanLyDuocPham.Controllers.QuanLyHeThong.CongNo
{
    class THUCHI_Control
    {
        DuocPhamDataContext db = new DuocPhamDataContext(staticClass.Conn);
        public void loadComboBox(ComboBox cmb)
        {

            cmb.Items.Clear();
            var types = db.THUCHI_Get_List();
            var typeorder = types.OrderBy(w => w.NGAY);
            foreach (var t in typeorder)
            {
                if (t.DA_XOA == false)
                    cmb.Items.Add(t);
            }
            cmb.SelectedValuePath = "MA_THUCHI";
           
            cmb.IsTextSearchEnabled = true;
            cmb.IsEditable = true;
            THUCHI_Get_ListResult temp = new THUCHI_Get_ListResult();

            temp.MA_THUCHI = "...";
            temp.LY_DO = "Thêm mới";
            cmb.Items.Add(temp);
        }

        public string loadID_moi()
        {
            string ID = "";

            int rowcount = db.THUCHI_Get_List().Count();
            if (rowcount == 0)
            {
                ID = "TC00000001";
            }
            else
            {
                for (int i = 1; i < rowcount+2; i++)
                {
                    string IDtam = "TC";
                    for (int j = 0; j < (8 - i.ToString().Length); j++)
                        IDtam = IDtam + "0";
                    IDtam = IDtam + i;
                    Boolean kq = true;
                    var data = db.THUCHI_Get_List();
                    
                    foreach (var t in data)
                    {

                        if (t.MA_THUCHI.Trim() == IDtam.Trim())

                            kq = false;
                    }
                    if (kq == true)
                    {
                        ID = IDtam;
                        break;
                    }
                }

            }
            return ID;

        }


        public void addData(TextBox txtMa_ThuChi, ComboBox cmbNhanVien, ComboBox cmbNhaThuoc, TextBox txtLyDo, ComboBox cmbPTTT, DatePicker dtpNgay, TextBox txtSCT, TextBox txtMST, TextBox txtSo_Tien, TextBox txtDien_Giai, CheckBox chbDa_Xoa)
        {
            if (validateData(cmbNhanVien, cmbNhaThuoc, txtLyDo, cmbPTTT, txtSCT, txtMST, txtSo_Tien) == false)
                MessageBox.Show("Chưa nhập đủ thông tin!");
            else
            {
                var cmbNhanVienSelected = (NHANVIEN_Get_ListResult)cmbNhanVien.SelectedItem;
                var cmbNhaThuocSelected = (NHATHUOC_Get_ListResult)cmbNhaThuoc.SelectedItem;

                db.THUCHI_Insert(txtMa_ThuChi.Text, cmbNhanVienSelected.MA_NHANVIEN, cmbNhaThuocSelected.MA_NHATHUOC, txtLyDo.Text, cmbPTTT.SelectedValue.ToString(), dtpNgay.SelectedDate, txtSCT.Text, txtMST.Text, int.Parse(txtSo_Tien.Text), txtDien_Giai.Text, chbDa_Xoa.IsChecked);
                MessageBox.Show("Đã thêm thành công");
            }
        }
        private bool validateData(ComboBox cmbNhanVien, ComboBox cmbNhaThuoc, TextBox txtLyDo, ComboBox cmbPTTT, TextBox txtSCT, TextBox txtMST, TextBox txtSo_Tien)
        {
            if (cmbNhanVien.SelectedItem == null || cmbNhaThuoc.SelectedItem == null || txtLyDo.Text.Trim() == "" || cmbPTTT.SelectedItem == null || txtSCT.Text.Trim() == "" || txtMST.Text.Trim() == "" || txtSo_Tien.Text.Trim() == "")
            {
                return false;
            }
            return true;
        }

        public void loadData(ListView List)
        {

            var types = db.THUCHI_Get_List();
            var typeorder = types.OrderBy(w => w.NGAY);
            foreach (var t in typeorder)
            {
                if (t.DA_XOA == false)
                    List.Items.Add(t);
            }

        }

        public void loadData( DataPager dp)
        {
            ObservableCollection<Object> dpSource = new ObservableCollection<Object>();

            var types = db.THUCHI_Get_List().Join(db.NHATHUOC_Get_List(),p=>p.MA_NHATHUOC,c=>c.MA_NHATHUOC,(p,c)=> new { p.MA_THUCHI, p.NGAY,c.TEN_NHATHUOC, p.LY_DO, p.SO_TIEN, p.SCT, p.DA_XOA});
            var typeorder = types.OrderBy(w => w.NGAY);
            int i = 1;
            foreach (var u in typeorder)
            {
                if (u.DA_XOA == false)
                {
                    dpSource.Add(new { STT = i, u.MA_THUCHI, u.NGAY, u.TEN_NHATHUOC, u.LY_DO, u.SO_TIEN, u.SCT });
                    i++;

                }
            }
            dp.ItemsSource = dpSource;
            dp.ItemsPerPage = 10;
        }

        public void updateData(TextBox txtMa_ThuChi, ComboBox cmbNhanVien, ComboBox cmbNhaThuoc, TextBox txtLyDo, ComboBox cmbPTTT, DatePicker dtpNgay, TextBox txtSCT, TextBox txtMST, TextBox txtSo_Tien, TextBox txtDien_Giai, CheckBox chbDa_Xoa)
        {
            if (validateData(cmbNhanVien, cmbNhaThuoc, txtLyDo, cmbPTTT, txtSCT, txtMST, txtSo_Tien) == false)
                MessageBox.Show("Chưa nhập đủ thông tin!");
            else
            {
                var cmbNhanVienSelected = (NHANVIEN_Get_ListResult)cmbNhanVien.SelectedItem;
                var cmbNhaThuocSelected = (NHATHUOC_Get_ListResult)cmbNhaThuoc.SelectedItem;

                db.THUCHI_Update(txtMa_ThuChi.Text, txtMa_ThuChi.Text, cmbNhanVienSelected.MA_NHANVIEN, cmbNhaThuocSelected.MA_NHATHUOC, txtLyDo.Text, cmbPTTT.SelectedItem.ToString(), dtpNgay.SelectedDate, txtSCT.Text, txtMST.Text, int.Parse(txtSo_Tien.Text), txtDien_Giai.Text, chbDa_Xoa.IsChecked);
                MessageBox.Show("Đã cập nhật!");
            }
        }

        public string deleteData(string ID_Delete, ListView List, string error)
        {

            error = "";
            var sl = (THUCHI_Get_ListResult)List.SelectedItem;
            db.THUCHI_Update(ID_Delete, sl.MA_THUCHI, sl.MA_NHANVIEN, sl.MA_NHATHUOC, sl.LY_DO, sl.PTTT, sl.NGAY, sl.SCT, sl.MST, sl.SO_TIEN, sl.DIEN_GIAI, true);
            return error;

        }
        DanhMuc.NHANVIEN_Control ctrl1 = new DanhMuc.NHANVIEN_Control();
        DanhMuc.NHATHUOC_Control ctrl2 = new DanhMuc.NHATHUOC_Control();

        public void loadForm_Edit(string ID_Edit, TextBox txtMa_ThuChi, ComboBox cmbNhanVien, ComboBox cmbNhaThuoc, TextBox txtLyDo, ComboBox cmbPTTT, DatePicker dtpNgay, TextBox txtSCT, TextBox txtMST, TextBox txtSo_Tien, TextBox txtDien_Giai)
        {

            var sl = (THUCHI_GetByMaThuchiResult)db.THUCHI_GetByMaThuchi(ID_Edit).Single();


            txtMa_ThuChi.Text = sl.MA_THUCHI;

            ctrl1.loadComboBox(cmbNhanVien);
            int t = 0;
            foreach (var i in cmbNhanVien.Items)
            {
                var temp = (NHANVIEN_Get_ListResult)i;
                if (temp.MA_NHANVIEN == sl.MA_NHANVIEN)
                {
                    break;
                }
                t++;
            }
            cmbNhanVien.SelectedIndex = t;

            ctrl2.loadComboBox(cmbNhaThuoc);
            int u = 0;
            foreach (var i in cmbNhaThuoc.Items)
            {
                var temp = (NHATHUOC_Get_ListResult)i;
                if (temp.MA_NHATHUOC == sl.MA_NHATHUOC)
                {
                    break;
                }
                u++;
            }
            cmbNhaThuoc.SelectedIndex = u;
            cmbPTTT.Items.Add("TM");
            cmbPTTT.Items.Add("CK");
            if (sl.PTTT.Trim() == null)
            {
                int v = 0;

                foreach (var i in cmbPTTT.Items)
                {

                    if (i.ToString() == sl.PTTT)
                    {
                        break;
                    }
                    v++;
                }
                cmbPTTT.SelectedIndex = v;

            }
            else
                cmbPTTT.SelectedItem = null;
            dtpNgay.SelectedDate = sl.NGAY;
            txtDien_Giai.Text = sl.DIEN_GIAI;
            txtLyDo.Text = sl.LY_DO;
            txtMST.Text = sl.MST;
            txtSCT.Text = sl.SCT;
            txtSo_Tien.Text = sl.SO_TIEN.ToString();



        }
    }
}